package com.demo.Controller;

import java.util.List;


import javax.ws.rs.DefaultValue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.demo.entity.Student;
import com.demo.entity.StudentInfo;
import com.demo.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/Student")
public class StudentController {
	@Autowired
	private StudentService studentService;
	
	@RequestMapping(value="/addStudent")
	public String addStudent(Student student) throws Exception{
		int addStudent = studentService.addStudent(student);
		if(addStudent>0)
			return "ok";
		else
			return "error";
	}
	
	@RequestMapping(value="/deleteStudentById/{id}")
	public String deleteStudentById(@PathVariable String id) throws Exception{
		int deleteStudentById = studentService.deleteStudentById(id);
		if(deleteStudentById>0)
			return "ok";
		else
			return "error";
	}
	
	@RequestMapping(value="/findStudentById/{id}")
	public Student findStudentById(@PathVariable String id) throws Exception{
		Student selectStudentById = studentService.selectStudentById(id);
		return selectStudentById;
	}
	
	@RequestMapping(value="/updateStudentById")
	public String updateStudentById(Student student) throws Exception{
		int updateStudent = studentService.updateStudent(student);
		if(updateStudent>0)
			return "ok";
		else
			return "error";
	}
	
	@RequestMapping(value="/findAllStudent/{pageNum}/{pageSize}")
	public PageInfo<Student> findAllStudent(@PathVariable @DefaultValue("1") Integer pageNum , @PathVariable @DefaultValue("5") Integer pageSize)throws Exception{
		PageHelper.startPage(pageNum, pageSize);
		List<Student> selectAllStudent = studentService.selectAllStudent();
		PageInfo<Student> pageInfo = new PageInfo<>(selectAllStudent);
		return pageInfo;
	}
	//查询学生及班级信息
	@RequestMapping(value="/findStudntClassInfo/{pageNum}/{pageSize}")
	public PageInfo<Student> findStudntClassInfo(@PathVariable @DefaultValue("1") Integer pageNum , @PathVariable @DefaultValue("5") Integer pageSize) throws Exception{
		PageHelper.startPage(pageNum, pageSize);
		List<Student> selectAllStudent = studentService.selectAllStudentInfo();
		PageInfo<Student> pageInfo = new PageInfo<>(selectAllStudent);
		return pageInfo;
	}
	
	//2
	@RequestMapping(value="/findStudntInfo/{pageNum}/{pageSize}")
	public PageInfo<StudentInfo> findStudntInfo(@PathVariable @DefaultValue("1") Integer pageNum , @PathVariable @DefaultValue("5") Integer pageSize) throws Exception{
		PageHelper.startPage(pageNum, pageSize);
		List<StudentInfo> selectAllStudentInfoE = studentService.selectAllStudentInfoE();
		PageInfo<StudentInfo> pageInfo = new PageInfo<StudentInfo>(selectAllStudentInfoE);
		return pageInfo;
	}
	////根据学生姓名,id和班级名称查找学生 
	@RequestMapping(value="/findStudentByNamesId",method=RequestMethod.GET)
	public PageInfo<StudentInfo> findStudentByNamesId(StudentInfo studentInfo, @DefaultValue("1") Integer pageNum,@DefaultValue("5") Integer pageSize) throws Exception{
		PageHelper.startPage(pageNum, pageSize);
		//去除空格
		studentInfo.setClassName(studentInfo.getClassName().trim());
		studentInfo.setName(studentInfo.getName().trim());
		studentInfo.setId(studentInfo.getId().trim());
		List<StudentInfo> findStudentByNamesId = studentService.selectStudentByNamesId(studentInfo);
		
		PageInfo<StudentInfo> pageInfo = new PageInfo<>(findStudentByNamesId);
		return pageInfo;
	}
}
